layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var element = layui.element;
    var form = layui.form;

    $.ajax(Feng.ctxPath + '/metadata/list', {
        success: function (data) {
            data = data.data;
            //为数据清洗元数据下拉列表框赋值
            $("#metadataName1").empty();
            $("#metadataName1").append("<option value=''>请选择元数据</option>");
            for (let i = 0; i < data.length; i++) {
                $("#metadataName1").append(new Option(data[i].metadataName, data[i].metadataId))
            }
            //重新渲染
            form.render("select");
        }
    })

    /**
     * 清洗规则标准下拉列表框赋值
     */
    // $.ajax(Feng.ctxPath + '/cleanRule/list', {
    //     success: function (data) {
    //         data = data.data;
    //         // console.log(data)
    //         $("#cleanRuleName").empty();
    //         $("#cleanRuleName").append("<option value=''>请选择清洗标准</option>");
    //         // console.log(data)
    //         for (let i = 0; i < data.length; i++) {
    //             $("#cleanRuleName").append(new Option(data[i].dataCleanRuleName, data[i].dataCleanRuleId))
    //         }
    //         //重新渲染
    //         form.render("select");
    //     }
    // })

    /**
     * 数据清洗表管理
     */
    var DataClean = {
        tableId: "dataCleanTable"
    };

    /**
     * 初始化表格的列
     */
    DataClean.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dataDataCleanId', hide: true, title: '数据清洗id'},
            {field: 'metadataName', sort: false, title: '元数据名称', align: "center"},
            {field: 'cleanRuleName', sort: false, title: '清洗标准', align: "center"},
            {field: 'problemDataNumber', sort: false, title: '问题数据量', align: "center"},
            {field: 'idDelete', sort: false, title: '是否删除；0代表删除；1代表可用', hide: true},
            {align: 'center', toolbar: '#tableBar1', title: '操作', align: "center"}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DataClean.search = function () {
        var queryData = {};

        if ($("#metadataName1 option:selected").text() == "请选择元数据") {
            queryData['metadataName'] = ""
        } else {
            queryData['metadataName'] = $("#metadataName1 option:selected").text()
        }

        if ($("#cleanRuleName option:selected").text() == "请选择清洗标准") {
            queryData['cleanRuleName'] = ""
        } else {
            queryData['cleanRuleName'] = $("#cleanRuleName option:selected").text()
        }


        table.reload(DataClean.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 清洗功能
     */
    $("#btnClean").click(function () {

        var metadataId = $("#metadataName1").val()

        if (metadataId == "" || metadataId == null) {
            Feng.error("请选择一条下拉框元数据检查")
            return;
        }


        //发送请求进行质量检查
        $.ajax(Feng.ctxPath + "/dataClean/cleanData", {
            async: false,
            data: {
                metadataId: metadataId,
            },
            success: function (data) {
                if (data == 200) {
                    Feng.success("清洗成功")
                    table.reload(DataClean.tableId, {
                        where: {}, page: {curr: 1}
                    });
                }
                if (data == 500) {
                    Feng.error("清洗失败")
                }
            },
            error: function () {
                Feng.error("清洗失败")
            }
        })


    })

    /**
     * 工具栏查看数据
     */
    DataClean.onLookData1 = function (data) {
        window.location.href = Feng.ctxPath + '/cleanDetail?dataDataCleanId=' + data.dataDataCleanId
    }

    /**
     * 工具栏清洗功能
     */
    DataClean.onDataClean = function (data) {
        console.log(data)
        //发送请求进行质量检查
        $.ajax(Feng.ctxPath + "/dataClean/cleanData", {
            async: false,
            data: {
                metadataName: data.metadataName,
            },
            success: function (data) {
                if (data == 200) {
                    Feng.success("清洗成功")
                    table.reload(DataClean.tableId, {
                        where: {}, page: {curr: 1}
                    });
                }
                if (data == 500) {
                    Feng.error("清洗失败")
                }
            },
            error: function () {
                Feng.error("清洗失败")
            }
        })
    }

    // 渲染表格
    var tableResult1 = table.render({
        elem: '#' + DataClean.tableId,
        url: Feng.ctxPath + '/dataClean/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: "line",
        cols: DataClean.initColumn(),
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff')
            table_data1 = res.data;
        }
    });

    /**
     * 获取表格复选框内的值
     */
    var table_data1 = new Array();
    var ids1 = new Array();
    table.on('checkbox(' + DataClean.tableId + ')', function (obj) {
        console.log(obj)
        if (obj.checked == true) {
            if (obj.type == 'one') {
                ids1.push(obj.data.dataQualityCheckId);
            } else {
                for (var i = 0; i < table_data1.length; i++) {
                    ids1.push(table_data1[i].dataQualityCheckId);
                }
            }
        } else {
            if (obj.type == 'one') {
                for (var i = 0; i < ids1.length; i++) {
                    if (ids1[i] == obj.data.dataQualityCheckId) {
                        ids1.splice(i, 1);
                    }
                }
            } else {
                for (var i = 0; i < ids1.length; i++) {
                    for (var j = 0; j < table_data1.length; j++) {
                        if (ids1[i] == table_data1[j].dataQualityCheckId) {
                            ids1.splice(i, 1);
                        }
                    }
                }
            }
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch1').click(function () {
        DataClean.search();
    });

    // 工具条点击事件
    table.on('tool(' + DataClean.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'lookData') {
            DataClean.onLookData1(data);
        } else if (layEvent === 'dataClean') {
            DataClean.onDataClean(data);
        }
    });
});
